#include "mainwindow.h"
#include <math.h>

int MainWindow::offM2(int j)
{
    int offM2[3];
    offM2[j]=0;
    for (int i=7*j;i<7*(j+1);i++)
        if(boxm2[i]->checkState()==Qt::Checked) offM2[j]++;
    return offM2[j];
}

void MainWindow::changeAllLineColorAfterTransimt(int i)
{
    if(signalValue2[i]!=-1)
    {
        if(signalValue2[i]>0)
        {
            if(i!=Error-1) f_con[i]->setStyleSheet(colorTrue1);
            else f_con[i]->setStyleSheet(colorFalse1);
        }
        else
        {
            if(i!=Error-1) f_con[i]->setStyleSheet(colorTrue0);
            else f_con[i]->setStyleSheet(colorFalse0);
        }
    }
    else f_con[i]->setStyleSheet(colorDefault);
}

void MainWindow::changeInformLineColorAfterTransmit(int i)
{
    if(!i)
    {
        if(signalValue2[i]!=-1)
        {
            if(signalValue2[i]>0)
            {
                if(i!=Error-1) f_B[i]->setStyleSheet(colorTrue1);
                else f_B[i]->setStyleSheet(colorFalse1);
            }
            else
            {
                if(i!=Error-1) f_B[i]->setStyleSheet(colorTrue0);
                else f_B[i]->setStyleSheet(colorFalse0);
            }
        }
        else f_B[i]->setStyleSheet(colorDefault);
    }
    else
    {
        if(signalValue2[i]!=-1)
        {
            if(signalValue2[i]>0)
            {
                if(i!=Error-1)
                {
                    f_B[i]->setStyleSheet(colorTrue1);
                    f_H[i-1]->setStyleSheet(colorTrue1);
                    f_J[i-1]->setStyleSheet(colorTrue1);
                }
                else
                {
                    f_B[i]->setStyleSheet(colorFalse1);
                    f_H[i-1]->setStyleSheet(colorFalse1);
                    f_J[i-1]->setStyleSheet(colorFalse1);
                }
            }
            else
            {
                if(i!=Error-1)
                {
                    f_B[i]->setStyleSheet(colorTrue0);
                    f_H[i-1]->setStyleSheet(colorTrue0);
                    f_J[i-1]->setStyleSheet(colorTrue0);
                }
                else
                {
                    f_B[i]->setStyleSheet(colorFalse0);
                    f_H[i-1]->setStyleSheet(colorFalse0);
                    f_J[i-1]->setStyleSheet(colorFalse0);
                }

            }
        }
        else
        {
            f_B[i]->setStyleSheet(colorDefault);
            f_H[i-1]->setStyleSheet(colorDefault);
            f_J[i-1]->setStyleSheet(colorDefault);
        }
    }
}

void MainWindow::changeBox2LineColor(int i){
    if(boxm2[i]->checkState()==Qt::Checked){
        switch ((int)i/7)
        {
            case 0:
            {
                if(signalValue2[i%7]!=-1)
                {
                    if(signalValue2[i%7]>0)
                    {
                        if((i%7)!=Error-1)
                        {
                            fm4[i%7]->setStyleSheet(colorTrue1);
                            fme4[i%7]->setStyleSheet(colorTrue1);
                        }
                        else
                        {
                            fm4[i%7]->setStyleSheet(colorFalse1);
                            fme4[i%7]->setStyleSheet(colorFalse1);
                        }
                    }
                    else
                    {
                        if((i%7)!=Error-1)
                        {
                            fm4[i%7]->setStyleSheet(colorTrue0);
                            fme4[i%7]->setStyleSheet(colorTrue0);
                        }
                        else
                        {
                            fm4[i%7]->setStyleSheet(colorFalse0);
                            fme4[i%7]->setStyleSheet(colorFalse0);
                        }

                    }
                    M4+=signalValue2[i%7];
                    changeM2LineColor((int)i/7);
                }
                else
                {
                    fm4[i%7]->setStyleSheet(colorDefault);
                    fme4[i%7]->setStyleSheet(colorDefault);
                }
                break;
            }
            case 1:
            {
                if(signalValue2[i%7]!=-1)
                {
                    if(signalValue2[i%7]>0)
                    {
                        if((i%7)!=Error-1)
                        {
                            fm5[i%7]->setStyleSheet(colorTrue1);
                            fme5[i%7]->setStyleSheet(colorTrue1);
                        }
                        else
                        {
                            fm5[i%7]->setStyleSheet(colorFalse1);
                            fme5[i%7]->setStyleSheet(colorFalse1);
                        }
                    }
                    else
                    {
                        if((i%7)!=Error-1)
                        {
                            fm5[i%7]->setStyleSheet(colorTrue0);
                            fme5[i%7]->setStyleSheet(colorTrue0);
                        }
                        else
                        {
                            fm5[i%7]->setStyleSheet(colorFalse0);
                            fme5[i%7]->setStyleSheet(colorFalse0);
                        }
                    }
                    M5+=signalValue2[i%7];
                    changeM2LineColor((int)i/7);
                }
                else
                {
                    fm5[i%7]->setStyleSheet(colorDefault);
                    fme5[i%7]->setStyleSheet(colorDefault);
                }
                break;
            }
            case 2:
            {
                if(signalValue2[i%7]!=-1)
                {
                    if(signalValue2[i%7]>0)
                    {
                        if((i%7)!=Error-1)
                        {
                            fm6[i%7]->setStyleSheet(colorTrue1);
                            fme6[i%7]->setStyleSheet(colorTrue1);
                        }
                        else
                        {
                            fm6[i%7]->setStyleSheet(colorFalse1);
                            fme6[i%7]->setStyleSheet(colorFalse1);
                        }
                    }
                    else
                    {
                        if((i%7)!=Error-1)
                        {
                            fm6[i%7]->setStyleSheet(colorTrue0);
                            fme6[i%7]->setStyleSheet(colorTrue0);
                        }
                        else
                        {
                            fm6[i%7]->setStyleSheet(colorFalse0);
                            fme6[i%7]->setStyleSheet(colorFalse0);
                        }

                    }
                    M6+=signalValue2[i%7];
                    changeM2LineColor((int)i/7);
                }
                else
                {
                    fm6[i%7]->setStyleSheet(colorDefault);
                    fme6[i%7]->setStyleSheet(colorDefault);
                }
                break;
            }
        }
    }
    else{
        switch ((int)i/7)
        {
            case 0:
            {
                if(signalValue2[i%7]!=-1)
                {
                    fm4[i%7]->setStyleSheet(colorDefault);
                    fme4[i%7]->setStyleSheet(colorDefault);
                    M4-=signalValue2[i%7];
                    changeM2LineColor((int)i/7);
                }
                else
                {
                    fm4[i%7]->setStyleSheet(colorDefault);
                    fme4[i%7]->setStyleSheet(colorDefault);
                }
                break;
            }
            case 1:
            {
                if(signalValue2[i%7]!=-1)
                {
                    fm5[i%7]->setStyleSheet(colorDefault);
                    fme5[i%7]->setStyleSheet(colorDefault);
                    M5-=signalValue2[i%7];
                    changeM2LineColor((int)i/7);
                }
                else
                {
                    fm5[i%7]->setStyleSheet(colorDefault);
                    fme5[i%7]->setStyleSheet(colorDefault);
                }
                break;
            }
            case 2:
            {
                if(signalValue2[i%7]!=-1)
                {
                    fm6[i%7]->setStyleSheet(colorDefault);
                    fme6[i%7]->setStyleSheet(colorDefault);
                    M6-=signalValue2[i%7];
                    changeM2LineColor((int)i/7);
                }
                else
                {
                    fm6[i%7]->setStyleSheet(colorDefault);
                    fme6[i%7]->setStyleSheet(colorDefault);
                }
                break;
            }
        }
    }
}

void MainWindow::changeM2LineColorAfterInput(){
    M4=0;
    M5=0;
    M6=0;
    for(int i=0;i<21;i++)
    {
        if(boxm2[i]->checkState()==Qt::Checked){
            switch ((int)i/7)
            {
                case 0:
                {
                    if(signalValue2[i%7]!=-1)
                    {
                        if(signalValue2[i%7]>0)
                        {
                            if((i%7)!=Error-1)
                            {
                                fm4[i%7]->setStyleSheet(colorTrue1);
                                fme4[i%7]->setStyleSheet(colorTrue1);
                            }
                            else
                            {
                                fm4[i%7]->setStyleSheet(colorFalse1);
                                fme4[i%7]->setStyleSheet(colorFalse1);
                            }
                        }
                        else
                        {
                            if((i%7)!=Error-1)
                            {
                                fm4[i%7]->setStyleSheet(colorTrue0);
                                fme4[i%7]->setStyleSheet(colorTrue0);
                            }
                            else
                            {
                                fm4[i%7]->setStyleSheet(colorFalse0);
                                fme4[i%7]->setStyleSheet(colorFalse0);
                            }
                        }
                        if(signalValue2[i%7]>0) M4+=signalValue2[i%7];
                        changeM2LineColor((int)i/7);
                    }
                    else
                    {
                        fm4[i%7]->setStyleSheet(colorDefault);
                        fme4[i%7]->setStyleSheet(colorDefault);
                    }
                    break;
                }
                case 1:
                {
                    if(signalValue2[i%7]!=-1)
                    {
                        if(signalValue2[i%7]>0)
                        {
                            if((i%7)!=Error-1)
                            {
                                fm5[i%7]->setStyleSheet(colorTrue1);
                                fme5[i%7]->setStyleSheet(colorTrue1);
                            }
                            else
                            {
                                fm5[i%7]->setStyleSheet(colorFalse1);
                                fme5[i%7]->setStyleSheet(colorFalse1);
                            }
                        }
                        else
                        {
                            if((i%7)!=Error-1)
                            {
                                fm5[i%7]->setStyleSheet(colorTrue0);
                                fme5[i%7]->setStyleSheet(colorTrue0);
                            }
                            else
                            {
                                fm5[i%7]->setStyleSheet(colorFalse0);
                                fme5[i%7]->setStyleSheet(colorFalse0);
                            }
                        }
                        if(signalValue2[i%7]>0) M5+=signalValue2[i%7];
                        changeM2LineColor((int)i/7);
                    }
                    else
                    {
                        fm5[i%7]->setStyleSheet(colorDefault);
                        fme5[i%7]->setStyleSheet(colorDefault);
                    }
                    break;
                }
                case 2:
                {
                    if(signalValue2[i%7]!=-1)
                    {
                        if(signalValue2[i%7]>0)
                        {
                            if((i%7)!=Error-1)
                            {
                                fm6[i%7]->setStyleSheet(colorTrue1);
                                fme6[i%7]->setStyleSheet(colorTrue1);
                            }
                            else
                            {
                                fm6[i%7]->setStyleSheet(colorFalse1);
                                fme6[i%7]->setStyleSheet(colorFalse1);
                            }
                        }
                        else
                        {
                            if((i%7)!=Error-1)
                            {
                                fm6[i%7]->setStyleSheet(colorTrue0);
                                fme6[i%7]->setStyleSheet(colorTrue0);
                            }
                            else
                            {
                                fm6[i%7]->setStyleSheet(colorFalse0);
                                fme6[i%7]->setStyleSheet(colorFalse0);
                            }
                        }
                        if(signalValue2[i%7]>0) M6+=signalValue2[i%7];
                        changeM2LineColor((int)i/7);
                    }
                    else
                    {
                        fm6[i%7]->setStyleSheet(colorDefault);
                        fme6[i%7]->setStyleSheet(colorDefault);
                    }
                    break;
                }
            }
        }
    }
}

void MainWindow::changeM2LineColor(int i)
{
    switch (i)
    {
        case 0:
        {
            signalValue3[0]=M4%2;
            if(signalValue3[0])
            {
                f_C[0]->setStyleSheet(colorTrue1);
                f_D[0]->setStyleSheet(colorTrue1);
                f_E[0]->setStyleSheet(colorTrue1);
            }
            else
            {
                if (offM2(i))
                {
                    f_C[0]->setStyleSheet(colorTrue0);
                    f_D[0]->setStyleSheet(colorTrue0);
                    f_E[0]->setStyleSheet(colorTrue0);
                }
                else
                {
                    f_C[0]->setStyleSheet(colorDefault);
                    f_D[0]->setStyleSheet(colorDefault);
                    f_E[0]->setStyleSheet(colorDefault);
                }
            }
            break;
        }
        case 1:
        {
            signalValue3[1]=M5%2;
            if(signalValue3[1])
            {
                f_C[1]->setStyleSheet(colorTrue1);
                f_D[1]->setStyleSheet(colorTrue1);
                f_E[1]->setStyleSheet(colorTrue1);
            }
            else
            {
                if (offM2(i))
                {
                    f_C[1]->setStyleSheet(colorTrue0);
                    f_D[1]->setStyleSheet(colorTrue0);
                    f_E[1]->setStyleSheet(colorTrue0);
                }
                else
                {
                    f_C[1]->setStyleSheet(colorDefault);
                    f_D[1]->setStyleSheet(colorDefault);
                    f_E[1]->setStyleSheet(colorDefault);
                }
            }
            break;
        }
        case 2:
        {
            signalValue3[2]=M6%2;
            if(signalValue3[2])
            {
                f_C[2]->setStyleSheet(colorTrue1);
                f_D[2]->setStyleSheet(colorTrue1);
                f_E[2]->setStyleSheet(colorTrue1);
            }
            else
            {
                if (offM2(i))
                {
                    f_C[2]->setStyleSheet(colorTrue0);
                    f_D[2]->setStyleSheet(colorTrue0);
                    f_E[2]->setStyleSheet(colorTrue0);
                }
                else
                {
                    f_C[2]->setStyleSheet(colorDefault);
                    f_D[2]->setStyleSheet(colorDefault);
                    f_E[2]->setStyleSheet(colorDefault);
                }
            }
            break;
        }
    }
    changeGradeMistakeLineColor();
}

void MainWindow::changeGradeMistakeLineColor()
{
    gradeMistake=0;
    for(int i=0;i<3;i++)
    {
        if(signalValue3[i]>=0)
            gradeMistake+=signalValue3[i]*pow(2,i);
    }

    if(gradeMistake==-1)
    {
        for(int i=0;i<3;i++)
        {
            f_K[i]->setStyleSheet(colorDefault);
            f_L[i]->setStyleSheet(colorDefault);
            f_M[i]->setStyleSheet(colorDefault);
            mistakeDC[i]=-1;
        }
        f_K[3]->setStyleSheet(colorDefault);
        mistakeDC[3]=-1;
    }
    else
    {
        for(int i=0;i<3;i++)
        {
            f_K[i]->setStyleSheet(colorTrue0);
            f_L[i]->setStyleSheet(colorTrue0);
            f_M[i]->setStyleSheet(colorTrue0);
            mistakeDC[i]=0;
        }
        f_K[3]->setStyleSheet(colorTrue0);
        mistakeDC[3]=0;

        switch(gradeMistake)
        {
            case 3:
            {
                f_K[0]->setStyleSheet(colorTrue1);
                f_L[0]->setStyleSheet(colorTrue1);
                f_M[0]->setStyleSheet(colorTrue1);
                mistakeDC[0]=1;
                break;
            }
            case 5:
            {
                f_K[1]->setStyleSheet(colorTrue1);
                f_L[1]->setStyleSheet(colorTrue1);
                f_M[1]->setStyleSheet(colorTrue1);
                mistakeDC[1]=1;
                break;
            }
            case 6:
            {
                f_K[2]->setStyleSheet(colorTrue1);
                f_L[2]->setStyleSheet(colorTrue1);
                f_M[2]->setStyleSheet(colorTrue1);
                mistakeDC[2]=1;
                break;
            }
            case 7:
            {
                f_K[3]->setStyleSheet(colorTrue1);
                mistakeDC[3]=1;
                break;
            }
        }
    }
    changeOutputLineColor();
}

void MainWindow::changeOutputLineColor()
{
    for(int i=0;i<4;i++)
    {
        signalOutputValue[i]=(signalValue2[i]+mistakeDC[i])%2;
        if(signalOutputValue[i]) f_N[i]->setStyleSheet(colorTrue1);
        else f_N[i]->setStyleSheet(colorTrue0);
    }
}
